import java.util.ArrayList;
import java.util.List;

class NewState2 extends PlanetWars implements Cloneable{

	private NewState2 parent;
	private String gameState;
	
	public static final int NEUTRAL = 0;
	public static final int FRIENDLY = 1;
	

	Planet preActionSource = null;
	Planet preActionDest = null;

	public NewState2(String gameStateString) {
		super(gameStateString);
		setGameState(gameStateString);
	}
	public NewState2(String gameStateString, NewState2 parent){
		super(gameStateString);
		setGameState(gameStateString);
		setParent(parent);
	}

	public int getScore(int i){
		int j;
		switch (i) {
		case 1:
			j =  Heuristics2.scoring1(this);
			break;
		case 2:
			j = Heuristics2.scoringUsingStateDepth(this);
			break;
		default:
			j = Heuristics2.normalizedScore(this);
			break;
		}
		return j;
	}

	@Override
	public NewState2 clone(){
		return new NewState2(this.toString());
	}


	public String getGameState() {
		return gameState;
	}
	public void setGameState(String gameState) {
		this.gameState = gameState;
	}
	public NewState2 getParent() {
		return parent;
	}
	public void setParent(NewState2 parent) {
		this.parent = parent;
	}

	public void SendFleet(Planet source, Planet dest) {
		// TODO Auto-generated method stub
		preActionSource = source;
		preActionDest = dest;

		int fleetSent = ( source.NumShips() - source.GrowthRate() ) / 2;
		Planet newActionDest = null;
		//boolean neutral = false;

		
		GetPlanet(source.PlanetID()).RemoveShips(fleetSent);
		newActionDest = GetPlanet(dest.PlanetID());
		int shipsLeftover = newActionDest.NumShips() - fleetSent;

		if (shipsLeftover >= 0)
		{
			newActionDest.RemoveShips(fleetSent);
		}
		else
		{
			newActionDest.RemoveShips(newActionDest.NumShips());
			newActionDest.Owner(source.Owner());
			newActionDest.AddShips(-shipsLeftover);
		}
	}
	
	public List<NewState2> getChildren() {
		List<NewState2> children = new ArrayList<NewState2>();
		
		List<Planet> own = new ArrayList<Planet>();
		List<Planet> other = new ArrayList<Planet>();
		
		for(Planet p : Planets()){
			switch (p.Owner()) {
			case FRIENDLY:
				own.add(p);
				break;
			default:
				other.add(p);
				break;
			}
		}
		
		for(Planet ownP : own){
			for(Planet otherP : other){
				NewState2 tmp = this.clone();
				tmp.setParent(this);
				tmp.SendFleet(ownP, otherP);
				children.add(tmp);
			}
		}
		
		return children;
	}
	
	// Voor minimax
	public List<NewState2> getChildren(boolean isMyTurn) {
		List<NewState2> children = new ArrayList<NewState2>();
		
		List<Planet> own = new ArrayList<Planet>();
		List<Planet> neutral = new ArrayList<Planet>();
		List<Planet> enemy = new ArrayList<Planet>();
		for(Planet p : Planets()){
			switch (p.Owner()) {
			case FRIENDLY:
				own.add(p);
				break;
			case NEUTRAL:
				neutral.add(p);
				break;
			default:
				enemy.add(p);
				break;
			}
		}
		if (!isMyTurn)
		{
			List<Planet> temp = own;
			own = enemy;
			enemy = temp;
		}

		for(Planet ownP : own){
			for(Planet otherP : enemy){
				NewState2 tmp = this.clone();
				tmp.setParent(this);
				tmp.SendFleet(ownP, otherP);
				children.add(tmp);
			}
			for(Planet otherP : neutral){
				NewState2 tmp = this.clone();
				tmp.setParent(this);
				tmp.SendFleet(ownP, otherP);
				children.add(tmp);
			}
		}
		
		return children;
	}


}